import {
  doLogin,
  jqLogin,
  validateAccount,
  sendPhoneCode,
  validateCode,
  setPassword,
  registered,
  getUserInfo,
  getAddress,
  getUserCard,
  sendEPayPhoneCode
} from "@/service/getData";
import {
  setSessionStorage,
  getSessionStorage,
  removeSessionStorage,
  removeLocalStorage,
  getLocalStorage,
  setLocalStorage
} from "@/utils/mixin";
import { Toast } from "mint-ui";
const user = {
  state: {
    userInfo: null, //用户信息数据
    userId: null, //用户信息数据
    addressList: null //用户地址数据
  },
  mutations: {
    SET_USERINFO_DATA(state, userInfo) {
      state.userInfo = userInfo;
    },
    SET_ADDRESSLIST_DATA(state, addressList) {
      state.addressList = addressList;
    }
  },
  actions: {
    //登录
    DoLogin({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        doLogin(parameterData).then(
           response => {
          
            setSessionStorage("token", response.data.user_token);
            resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    },
    JqLogin({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        jqLogin(parameterData).then(
           response => {
            setSessionStorage("token", response.data.user_token);
            resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    },

    //验证用户账号是否可用
    ValidateAccount({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        validateAccount(parameterData).then(
          response => {
            resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    },
    //获取用户信息
    GetUserInfo({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        getUserInfo(parameterData).then(
          response => {
            commit("SET_USERINFO_DATA", response.data.userInfo);
            resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    },
    //获取验证码
    SendPhoneCode({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        sendPhoneCode(parameterData).then(
          response => {
            return resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    },
    ValidateCode({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        validateCode(parameterData).then(
          response => {
            return resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    },

    //注册账号
    Registered({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        registered(parameterData).then(
          response => {
            resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    },

    GetUserCard({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        getUserCard(parameterData).then(
          response => {
            resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    },
    SendEPayPhoneCode({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        sendEPayPhoneCode(parameterData).then(
          response => {
            resolve(response);
          },
          err => {
            reject(err);
          }
        );
      });
    }
  }
};

export default user;
